package com.guotong.sysUser;

import com.guotong.utils.R;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 *  登录相关
 * @author H
 */
@RestController
@RequestMapping("/sys/user")
public class SysLoginController {
    @PostMapping("/login")
    public Object login(@RequestBody SysUser sysUser, HttpServletRequest request){

        UsernamePasswordToken token = new UsernamePasswordToken(sysUser.getUsername(),sysUser.getPassword());

        Subject subject = SecurityUtils.getSubject();
        System.out.println(token);
        try {
            // 开始认证，这一步会跳到我们自定义的 Realm 中
            subject.login(token);
//            request.getSession().setAttribute("user",user);
            return R.ok("成功");
        }catch (Exception e){
            e.printStackTrace();
//            request.getSession().setAttribute("user",user);
            return R.error("失败");
        }
    }
}
